/*
 * RegistrarSession
 * Handles Close and open Regitration
 */
package edu.depaul.se.courseregistration.sessionbean;

import edu.depaul.se.courseregistration.jpa.courses.Section;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
 *
 * @author Group2
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class RegistrarEJB {

    @PersistenceContext(unitName="CourseRegistrationModulePU")
    private EntityManager em;
    
    //RegistrarSession Buisness API
    //Close and Open regiteration
       //@Override
    public int closeSectionRegistration(int sectionId) {
        int result = 0;
        Section section = em.createNamedQuery(Section.SECTION_QUERY_BY_ID, Section.class).
                    setParameter("sectionId", sectionId).getSingleResult();
        //check the section is open first
        if(section.getSectionStaus()==true){
            section.setSectionStaus(false);
            em.merge(section);         
        }
        else{
            result = 1; //fail
        }
        return result;
    }
     //@Override
    public int openSectionRegistration(int sectionId) {
        int result = 0; //success
        Section section = em.createNamedQuery(Section.SECTION_QUERY_BY_ID, Section.class).
                    setParameter("sectionId", sectionId).getSingleResult();
        if(section.getSectionStaus()== false){
            section.setSectionStaus(true);
            em.merge(section);         
        }
        else{
            result = 1; //fail
        }
        return result;  
    }
    
}
